﻿namespace dotnet_notes.Extensions
{
    /// <summary>
    /// HttpClient 扩展类
    /// </summary>
    public static class HttpClientExtension
    {
        /// <summary>
        /// 发送 GET 请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">请求的地址(可带查询参数)</param>
        /// <returns>响应字符串</returns>
        public static async Task<string> SendGetAsync(this HttpClient client, string url)
        {
            var response = await client.GetAsync(url);
            response.EnsureSuccessStatusCode();

            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送 JSON 请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">请求的地址</param>
        /// <param name="data">请求的数据(JSON)</param>
        /// <returns>响应字符串</returns>
        public static async Task<string> SendJsonAsync
            (this HttpClient client, string url, string data)
        {
            using var content = new StringContent(data, Encoding.UTF8, "application/json");

            var response = await client.PostAsync(url, content);
            response.EnsureSuccessStatusCode();

            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送表单请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">请求的地址</param>
        /// <param name="data">请求的数据(字典)</param>
        /// <returns>响应字符串</returns>
        public static async Task<string> SendFormAsync
            (this HttpClient client, string url, Dictionary<string, string> data)
        {
            using var content = new FormUrlEncodedContent(data);

            var response = await client.PostAsync(url, content);
            response.EnsureSuccessStatusCode();

            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送复杂表单请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">请求的地址</param>
        /// <param name="content">请求的数据(自己构造表单内容)</param>
        /// <returns></returns>
        public static async Task<string> SendFormAsync
            (this HttpClient client, string url, MultipartFormDataContent content)
        {
            var response = await client.PostAsync(url, content);
            response.EnsureSuccessStatusCode();

            return await response.Content.ReadAsStringAsync();
        }

        /// <summary>
        /// 发送单文件表单请求
        /// </summary>
        /// <param name="client"></param>
        /// <param name="url">请求的地址</param>
        /// <param name="filePath">本地文件路径</param>
        /// <param name="parmName">接口参数名称</param>
        /// <returns>响应字符串</returns>
        public static async Task<string> SendSingleFileAsync
            (this HttpClient client, string url, string filePath, string parmName)
        {
            using var fileStream = File.OpenRead(filePath);
            using var streamContent = new StreamContent(fileStream);

            using var content = new MultipartFormDataContent
            {
                { streamContent, parmName, Path.GetFileName(filePath) }
            };

            var response = await client.PostAsync(url, content);
            response.EnsureSuccessStatusCode();

            return await response.Content.ReadAsStringAsync();
        }
    }
}
